package cn.kpi.service.usualassessment.impl;

import java.util.List;
import java.util.Map;

import cn.kpi.entity.usualassseason.MzpdPassNoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.usualassessment.UserUATMzpdDao;
import cn.kpi.entity.usualassessment.UserUATBmTjEntity;
import cn.kpi.entity.usualassessment.UserUATMzpdEntity;
import cn.kpi.entity.usualassessment.UserUATMzpdSummaryEntity;
import cn.kpi.entity.usualassessment.UserUATQzEntity;
import cn.kpi.service.usualassessment.IUserUATMzpdService;

@Service
public class UserUATMzpdServiceImpl implements IUserUATMzpdService {

    @Autowired
    private UserUATMzpdDao userUATMzpdDao;

    @Transactional
    public int InsertQz(UserUATQzEntity entity) throws Exception {
        try {
            int result = 0;
            result += userUATMzpdDao.UpdateQzStatusEntity();
            result += userUATMzpdDao.InsertQzEntity(entity);
            if (result < 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return -1;
            }

            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    public UserUATQzEntity GetQzByStatus() throws Exception {
        try {
            return userUATMzpdDao.GetQzByStatus();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Transactional
    public int InsertMzpdEntity(UserUATMzpdEntity entity) throws Exception {
        try {
            int result = 0;
            result += userUATMzpdDao.InsertMzpdEntity(entity);
            int iext = userUATMzpdDao.GetMzpdSummaryCountByYear(entity.getUserid(), entity.getYear());
            if (iext > 0) {
                result += userUATMzpdDao.UpdateMzpdSummary(entity.getUserid(), entity.getYear(), entity.getQuarter(), entity.getZzpdjg(), entity.getDymzpyjg(),entity.getSzdw(),entity.getSzdwname());
            } else {
                result += userUATMzpdDao.InsertMzpdSummary(entity.getUserid(), entity.getYear(), entity.getQuarter(), entity.getZzpdjg(), entity.getDymzpyjg(),entity.getSzdw(),entity.getSzdwname());
            }

            if (result < 2) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return -1;
            }

            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    public List<UserUATMzpdEntity> GetMzpdListByWhere(String where) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdListByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int GetMzpdListCountByWhere(String where) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdListCountByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public UserUATMzpdEntity GetMzpdListById(Long mzpdid) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdListById(mzpdid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public int DeleteMzpdEntity(Long mzpdid) throws Exception {
        try {
            return userUATMzpdDao.DeleteMzpdEntity(mzpdid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public List<UserUATMzpdSummaryEntity> GetMzpdSummaryListByWhere(String where, int year) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdSummaryListByWhere(where, year);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int GetMzpdSummaryListCountByWhere(String where, int year) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdSummaryListCountByWhere(where, year);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public List<Map> GetPjOrgSumList(String where, int year, int quarter) throws Exception {
        try {
            return userUATMzpdDao.GetPjOrgSumList(where, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<UserUATMzpdEntity> GetUserMzpdListProcess(String where, int year, int quarter, int beginrow, int endrow) throws Exception {
        try {
            return userUATMzpdDao.GetUserMzpdListProcess(where, year, quarter, beginrow, endrow);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<UserUATMzpdEntity> GetOldUserMzpdListProcess(String where, int year, int quarter, int beginrow, int endrow) throws Exception {
        try {
            return userUATMzpdDao.GetOldUserMzpdListProcess(where, year, quarter, beginrow, endrow);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int GetUserMzpdListProcessCount(String where) throws Exception {
        try {
            return userUATMzpdDao.GetUserMzpdListProcessCount(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
    public int GetOldUserMzpdListProcessCount(String where) throws Exception {
        try {
            return userUATMzpdDao.GetOldUserMzpdListProcessCount(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int UpdateMzpdEntity(UserUATMzpdEntity entity) throws Exception {
        try {
            int result = 0;
            result += userUATMzpdDao.UpdateMzpdSummary(entity.getUserid(), entity.getYear(), entity.getQuarter(), entity.getZzpdjg(), entity.getDymzpyjg(),entity.getSzdw(),entity.getSzdwname());
            result += userUATMzpdDao.UpdateMzpdEntity(entity);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int UpdateUATMzpdStatus(Long mzpdid, Long userid, int status, int year, int quarter) throws Exception {
        try {
            int result = 0;
            result += userUATMzpdDao.UpdateMzpdStatus(mzpdid, status);
            result += userUATMzpdDao.UpdateUsasstempNodeMzpd(mzpdid, userid, year, quarter);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int UpdateUATMzpdStatusBm(Long orgid, int status, int year, int quarter) throws Exception {
        try {
            int result = 0;
            result += userUATMzpdDao.InsertOrgStatusEntity(orgid, year, quarter);
            result += userUATMzpdDao.UpdateMzpdStatusBm(orgid, year, quarter, status);
            result += userUATMzpdDao.UpdateUsasstempNodeMzpdBm(orgid, year, quarter, status);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int UpdateUATMzpdStatusBmNew(Long orgid, int status, int year, int quarter, Long orgstatustjid) throws Exception {
        try {
            int result = 0;
            if (orgstatustjid == 0) {
                result += userUATMzpdDao.InsertOrgStatusEntity(orgid, year, quarter);
            } else {
                result += userUATMzpdDao.UpdateOrgStatusEntityTj(orgstatustjid);
            }

            result += userUATMzpdDao.UpdateMzpdStatusBm(orgid, year, quarter, status);
            result += userUATMzpdDao.UpdateUsasstempNodeMzpdBm(orgid, year, quarter, status);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    public UserUATBmTjEntity GetBmTj(int year, Long orgid) throws Exception {
        try {
            return userUATMzpdDao.GetBmTj(year, orgid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public UserUATMzpdEntity GetMzpdEntityByYQU(Long userid, int year, int quarter) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdEntityByYQU(userid, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int GetOrgStatusCount(Long orgid, int year, int quarter) throws Exception {
        try {
            return userUATMzpdDao.GetOrgStatusCount(orgid, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public List<UserUATMzpdEntity> GetAllUserMzpdListProcess(String where,String where3, int year, int quarter) throws Exception {
        try {
            return userUATMzpdDao.GetAllUserMzpdListProcess(where,where3, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<UserUATMzpdEntity> GetOldAllUserMzpdListProcess(String where, String where3, int year, int quarter) throws Exception {
        try {
            return userUATMzpdDao.GetOldAllUserMzpdListProcess(where,where3, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<UserUATMzpdEntity> GetMzpdPassNoEntity(String where, int year, int quarter) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdPassNoEntity(where, year, quarter);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    @Override
    public List<UserUATMzpdEntity> GetMzpdPassNoEntity1(String where, int year) throws Exception {
        try {
            return userUATMzpdDao.GetMzpdPassNoEntity1(where, year);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Transactional
    public int UpdateMzpdpassEntity(List<MzpdPassNoEntity> entity, String where) throws Exception {
        try {
            int ret = 0;
            if (!where.equals("")) {
                ret = userUATMzpdDao.deleteMzpdEntityBatch(where);
            }
            return userUATMzpdDao.insertMzpdEntityBatch(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Override
    public List<UserUATMzpdEntity> getMzpdById(int mzpdpassnoid) {
        return userUATMzpdDao.getMzpdById(mzpdpassnoid);
    }

    @Override
    public int updateMzpd(String reason, String howtodo, String remark, int mzpdpassnoid) {
        return userUATMzpdDao.updateMzpd(reason, howtodo, remark, mzpdpassnoid);
    }

    @Override
    public int updateMzpdStatus(int status, Long orgid, int year, int quarter) {
        return  userUATMzpdDao.updateMzpdStatus(status,orgid,year,quarter);
    }

}
